package com.jianlong.job;

import java.text.ParseException;

import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class Timer {
	static SchedulerFactory sf;
	static {
		sf = new StdSchedulerFactory();
	}
	static Scheduler scheduler;
	public static final String ONE_MINUTE = "0 0/1 * * * ?";
	public static final String Five_SECONDS = "0/5 * * * * ?";

	public static void addJob(String jobName, Job job, String time) {
		try {
			scheduler = sf.getScheduler();
			JobDetail jobDetail = new JobDetail(jobName, "group", job
					.getClass());
			CronTrigger trigger = new CronTrigger(jobName, "trigger_group");
			trigger.setCronExpression(time);
			scheduler.scheduleJob(jobDetail, trigger);
			if (!scheduler.isShutdown()) {
				scheduler.start();
			}
		} catch (SchedulerException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	public static void removeJob() {
		if (scheduler != null) {
			try {
				if (scheduler.isStarted()) {
					scheduler.shutdown();
				}
			} catch (SchedulerException e) {
				e.printStackTrace();
			}
		}
	}
}
